package xyz.weizhi.rjwm.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.weizhi.rjwm.entity.Category;
import xyz.weizhi.rjwm.entity.vo.R;
import xyz.weizhi.rjwm.service.CategoryService;

import java.util.List;

/**
 * @Author milian
 * @Date 2021/9/25 0025 14:42
 */
@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    /**
     * @Author milian
     * @Description //查询菜品种类
     * @Date 14:47 2021/9/25 0025
     * @Param []
     * @return xyz.weizhi.rjwm.entity.vo.R<xyz.weizhi.rjwm.entity.Category>
     **/
    @GetMapping("/page")
    public R<Page> getCategoryPage(@RequestParam("page") long pageNum,long pageSize){

        Page<Category> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.orderByAsc(Category::getSort);
        categoryService.page(page,lqw);
        return R.success(page);
    }
    /**
     * @Author milian
     * @Description //根据id查询菜品种类
     * @Date 14:50 2021/9/25 0025
     * @Param []
     * @return xyz.weizhi.rjwm.entity.vo.R<xyz.weizhi.rjwm.entity.Category>
     **/
    @GetMapping("/{id}")
    public R<Category> queryCategoryById(@PathVariable long id){
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        Category category = categoryService.getById(id);
        return R.success(category);
    }
    /**
     * @Author milian
     * @Description //添加菜品种类
     * @Date 14:52 2021/9/25 0025
     * @Param []
     * @return xyz.weizhi.rjwm.entity.vo.R<java.lang.String>
     **/
    @PostMapping()
    public R<String> addCategory(@RequestBody Category category){
        boolean isSave = categoryService.save(category);
        if (isSave){
            return R.success("添加成功");
        }else {
            return R.error("添加失败");
        }

    }
    /**
     * @Author milian
     * @Description //编辑菜品种类
     * @Date 14:53 2021/9/25 0025
     * @Param []
     * @return xyz.weizhi.rjwm.entity.vo.R<java.lang.String>
     **/
    @PutMapping
    public R<String> editCategory(@RequestBody Category category){
        boolean b = categoryService.updateById(category);
        if (b){
            return R.success("修改成功");
        }else {
            return R.error("修改失败");
        }
    }
    /**
     * @Author milian
     * @Description //删除菜品种类
     * @Date 14:53 2021/9/25 0025
     * @Param []
     * @return xyz.weizhi.rjwm.entity.vo.R<java.lang.String>
     **/
    @DeleteMapping
    public R<String> deleCategory(String id){
        boolean b = categoryService.removeById(id);
        if (b){
            return R.success("删除成功");
        }else {
            return R.error("删除失败");
        }
    }

    /**
     * @Author milian
     * @Description //根据类型获取菜品
     * @Date 20:18 2021/9/25 0025
     * @Param [type]
     * @return xyz.weizhi.rjwm.entity.vo.R<java.util.List<xyz.weizhi.rjwm.entity.Category>>
     **/
    @GetMapping("/list")
    public R<List<Category>> getCategoryList(Integer type){
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(type!=null,Category::getType,type)
           .orderByAsc(Category::getSort)
           .orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(lqw);
        return R.success(list);
    }

}
